package com.example.arithmeticleetcode.leetcode.nov;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-11-11 09:25
 **/
public class Demo06 {

    public static void main(String[] args) {

//[[1, 1, 1, 0, 1, 0, 0, 1, 0, 0],
// [1, 0, 1, 0, 0, 0, 1, 1, 0, 1]]
        int[] arr1 = {2,1,2,0,1,0,1,2,0,1};
        int[] arr2 = {2,2,1,1};
        int[] arr3 = {1,1,1};
        int[] arr4 = {0,1,2,0,0,0,0,0,2,1,2,1,2};
        System.out.println(reconstructMatrix(5, 5, arr1));
        System.out.println(reconstructMatrix(2, 3, arr2));
        System.out.println(reconstructMatrix(2, 1, arr3));
        System.out.println(reconstructMatrix(9, 2, arr4));

    }

    public static List<List<Integer>> reconstructMatrix(int upper, int lower, int[] colsum) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> innerOneResult = new ArrayList<>();
        List<Integer> innerTwoResult = new ArrayList<>();
        int judgeUpper = upper, judgeLower = lower, judgeSum = upper + lower;
        int sumUpper = 0, sumLower = 0, sum = 0;
        for (int i : colsum) {
            sum += i;
            if (i == 2) {
                sumUpper++;
                sumLower++;
                innerOneResult.add(1);
                innerTwoResult.add(1);
                upper--;
                lower--;
            } else {
                innerOneResult.add(0);
                innerTwoResult.add(0);
            }
        }
        for (int i = 0, len = colsum.length; i < len; i++) {
            if (colsum[i] == 1) {
                if (upper > 0) {
                    sumUpper++;
                    innerOneResult.set(i, 1);
                    upper--;
                    continue;
                }
                if (lower > 0) {
                    sumLower++;
                    innerTwoResult.set(i, 1);
                    lower--;
                }
            }
        }
        if (sum != judgeSum || sumUpper != judgeUpper || sumLower != judgeLower) {
            return result;
        }
        result.add(innerOneResult);
        result.add(innerTwoResult);
        return result;
    }
}
